package com.woniuxy.ams.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniuxy.ams.dao.*;
import com.woniuxy.ams.entity.*;
import com.woniuxy.ams.service.ExamineListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 部门考核列表实现类
 */
@Service
public class ExamineListServiceImpl implements ExamineListService {

    @Autowired(required = false)
    ExamineListDao examineListDao;
    @Autowired(required = false)
    UserDao userDao;
    @Autowired(required = false)
    AppraisalTemplateDao appraisalTemplateDao;
    @Autowired(required = false)
    TemplateIdDao templateIdDao;
    @Autowired(required = false)
    ExaminelistAssessmentDao examinelistAssessmentDao;


    //查找所有的考核列表
    @Override
    public List<ExamineList> findAll(int departmentId) {
        List<ExamineList> list = examineListDao.findAll(departmentId);
            ArrayList<ExamineList> arrayList = new ArrayList<>();
            for (ExamineList examineList : list) {
                int id = examineList.getExaminee();
                int assessorId = examineList.getAssessor();
                User one = userDao.findOneById(id);
                User one1 = userDao.findOneById(assessorId);
                int templateId = examineList.getTemplateId();
                AppraisalTemplate template = appraisalTemplateDao.findTemplate(templateId);
                String name = one.getName();
                String name1 = one1.getName();
                examineList.setName(name);
                examineList.setAssessorName(name1);
                examineList.setAppraisalTemplate(template);
                arrayList.add(examineList);
            }
        return arrayList;
    }
    //查询所有部门信息传入封装的对象，有部门id传，没有则查询所有部门考核列表信息
    @Override
    public List<ExamineList> findAllExamineList(ExamineListFormInline examineListFormInline) {
        PageHelper.startPage(examineListFormInline.getCurrentPage(), examineListFormInline.getLimit());
        List<ExamineList> list = examineListDao.findAllExamineList(examineListFormInline);
            for (ExamineList examineList : list) {
                int id = examineList.getExaminee();
                int assessorId = examineList.getAssessor();
                User one = userDao.findOneById(id);
                User one1 = userDao.findOneById(assessorId);
                int templateId = examineList.getTemplateId();
                AppraisalTemplate template = appraisalTemplateDao.findTemplate(templateId);
                String name = one.getName();
                String name1 = one1.getName();
                examineList.setName(name);
                examineList.setAssessorName(name1);
                examineList.setAppraisalTemplate(template);
            }

        return list;
    }

    @Override
    public int updateStatus(ExamineList examineList) {
        return examineListDao.updateStatus(examineList);
    }

    //增加考核信息
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int insert(ExamineList examineList) {
        examineList.setSelfReviewStatus("否");
        int machineId = 1;
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if(hashCodeV < 0) {
            hashCodeV = - hashCodeV;
        }
        String uuid = machineId + String.format("%011d", hashCodeV);
        examineList.setAppraisalNo(uuid);

        int i = examineListDao.insert(examineList);
        Integer templateId = examineList.getTemplateId();
        AppraisalTemplate template = appraisalTemplateDao.findTemplate(templateId);
        ExamineList oneByExamineList = findOneExamineByExamineList(examineList);
        int id1 = oneByExamineList.getId();
        ArrayList<TemplateAssess> arrayList1 = new ArrayList<>();
        for (TemplateAssess templateAssess:template.getTemplateAssess()) {
            arrayList1.add(templateAssess);
        }

        examineListDao.insertExaminelistAssessment2(id1,arrayList1);
        List<ExaminelistAssessment> examinelistAssessmentByEid = examinelistAssessmentDao.findExaminelistAssessmentByEid(id1);
        List<RaterUser> raterUsers = examineListDao.findRaterUserByTemplateId(templateId);
        Map<Integer,Integer> map = new HashMap<>();
        for(ExaminelistAssessment examinelistAssessment : examinelistAssessmentByEid){
            map.put(examinelistAssessment.getAssessId(),examinelistAssessment.getId());
            for(RaterUser raterUser : raterUsers){
                if (raterUser.getAssessId() == examinelistAssessment.getAssessId()) {
                    raterUser.setAssessId(map.get(raterUser.getAssessId()));
                }
            }
        }
        examineListDao.insertRater2(raterUsers);
        return i;
    }

    //修改考核列表
    @Override
    public int updateExamineList(ExamineList examineList) {
        return examineListDao.updateExamineList(examineList);
    }

    //删除考核列表
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int deleteExamineList(ExamineList examineList) {
        Integer id = examineList.getId();
        List<Integer> list = examineListDao.findExaminelistAssessment2(id);
        examineListDao.deleteRater(list);
        examineListDao.deleteExaminelistAssessment(id);
        return examineListDao.deleteExamineList(examineList);
    }

    //查找一条职员的考核信息
    @Override
    public ExamineList findOneExamine(int id) {
        ExamineList one1 = examineListDao.findOne(id);
            int id1 = one1.getExaminee();
            int assessorId = one1.getAssessor();
            User one = userDao.findOneById(id1);
            User one2 = userDao.findOneById(assessorId);
            int templateId = one1.getTemplateId();
            AppraisalTemplate template = appraisalTemplateDao.findTemplate(templateId);
            String name = one.getName();
            String name1 = one2.getName();
            one1.setName(name);
            one1.setAssessorName(name1);
            one1.setAppraisalTemplate(template);
        return one1;
    }

    //通过参数examinelist对象来查找一条职员的考核信息
    public ExamineList findOneExamineByExamineList(ExamineList examlineList) {
        return examineListDao.findOneByExamineList(examlineList);
    }
    //修改别考核人分数
    @Override
    public int updateScroe(Rater rater) {
        return  examineListDao.updateScore(rater);
    }


}
